create proc dbo.p_DeleteExpiredScanHistoryData as
select
	distinct sessionid
into
	#x
from
	scanhistory
where
	datediff(day,scancomplete,current_timestamp)>30

delete from 
	scanhistory 
where
	sessionid in (
		select sessionid from #x)

delete from
	scanqueue
where
	sessionid in (
		select sessionid from #x)

drop table #x